package com.xiyi.shouxin.dao.admin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.xiyi.shouxin.dao.BaseRepository;
import com.xiyi.shouxin.dao.JdbcPreparedStatement;
import com.xiyi.shouxin.entity.admin.SystemConfig;
import com.xiyi.shouxin.entity.admin.mapper.SystemConfigMapper;
import com.xiyi.shouxin.exception.CommonException;
import com.xiyi.shouxin.exception.ErrorCode;

@Repository
public class SystemConfigRepository extends BaseRepository<SystemConfig> {

    public SystemConfigRepository() {
        super();
        super.stat = new JdbcPreparedStatement<>(SystemConfig.class);
    }

    public SystemConfig findByConfigName(String configName) throws CommonException {
        if (!StringUtils.hasText(configName)) {
            throw new CommonException(ErrorCode.INVALIDPARAMTER);
        }
        StringBuilder sb = new StringBuilder("SELECT ");
        sb.append(stat.GetQueryColumns());
        sb.append(" FROM ");
        sb.append(stat.GetTableName());
        sb.append(" WHERE ");
        sb.append(stat.GetColumnName("configName"));
        sb.append(" = ? LIMIT 1");
        try {
            SystemConfig config = template.queryForObject(sb.toString(), new Object[] { configName },
                    new SystemConfigMapper());
            return config;
        } catch (EmptyResultDataAccessException e) {

        }

        return null;
    }

    public void updateByConfigName(String configName, SystemConfig config) throws CommonException {
        if (!StringUtils.hasText(configName)) {
            throw new CommonException(ErrorCode.INVALIDPARAMTER);
        }
        List<Object> args = new ArrayList<>();
        List<String> sets = new ArrayList<>();
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(stat.GetTableName());
        if (StringUtils.hasText(config.getConfigType())) {
            String column = stat.GetColumnName("configType");
            if (column != null) {
                String set = column + " = ? ";
                sets.add(set);
                args.add(config.getConfigType());
            }
        }
        if (StringUtils.hasText(config.getConfigValue())) {
            String column = stat.GetColumnName("configValue");
            if (column != null) {
                String set = column + " = ? ";
                sets.add(set);
                args.add(config.getConfigValue());
            }
        }
        if (StringUtils.hasText(config.getConfigBrief())) {
            String column = stat.GetColumnName("configBrief");
            if (column != null) {
                String set = column + " = ? ";
                sets.add(set);
                args.add(config.getConfigBrief());
            }
        }
        if (!sets.isEmpty()) {
            sb.append(" SET ");
            sb.append(String.join(",", sets));
            sb.append(" ");
        }
        sb.append(" WHERE ");
        sb.append(stat.GetColumnName("configName"));
        sb.append(" = ?");
        args.add(configName);
        template.update(sb.toString(), args.toArray());
    }

    public List<SystemConfig> findByMultiConditionsByPage(String configName, Sort sort, int page, int size)
            throws Exception {
        List<Object> args = new ArrayList<>();
        List<String> conditions = new ArrayList<>();
        if (StringUtils.hasText(configName)) {
            conditions.add(stat.GetColumnName("configName") + " like ? ");
            args.add("%" + configName + "%");
        }
        return findByMultiConditionsByPage(conditions, args, sort, page, size, new SystemConfigMapper());
    }

    public int countByMultiConditions(String configName) {
        List<Object> args = new ArrayList<>();
        List<String> conditions = new ArrayList<>();
        if (StringUtils.hasText(configName)) {
            conditions.add(stat.GetColumnName("configName") + " like ? ");
            args.add("%" + configName + "%");
        }

        return countByMultiConditions(conditions, args);
    }
}
